package com.leyou.item.controller;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import com.netflix.discovery.converters.Auto;
import org.jboss.logging.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
   //查询分类列表
    @GetMapping("/list")
    public ResponseEntity<List<Category>> queryCategoryByPid(@RequestParam(value = "pid",defaultValue = "0") Long id) {

       List<Category> categoryList= categoryService.queryCategoryByPid(id);
       if(categoryList==null||categoryList.size()==0){
           //错误，返回不存在状态码
       // throw new LyException(ExceptionEnum.CATEGORY_CANNOT_BE_NULL);
           return new ResponseEntity<>(HttpStatus.NOT_FOUND);
       }
        return ResponseEntity.ok(categoryList);
    }

    @GetMapping("/bid/{bid}")//获取回显商品的分类cids
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid")Long bid){
        List<Category> categoryList=categoryService.queryByBrandId(bid);
        if (categoryList==null||categoryList.size()<1){
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(categoryList);

    }

}
